#coding=utf-8

import webbrowser
import Tkinter
from base_client import base_client

def predicate_loginform( form ):
    if form.attrs.has_key("id") and form.attrs["id"]=="notLogin":
        return True
    return False

class client(base_client):
    def __init__(self, config=None ):
        if None==config:
            config = {
                      "blog_user":"",
                      "blog_password":"",
                      "microblog_user":"",
                      "microblog_password":"",
                      "x_cms_flag":"允许所有人评论"
            }
        base_client.__init__( self, config )

    def __blog_login(self):
        self.browser.open( 'http://blog.sina.com.cn' )
        self.browser.select_form( predicate=predicate_loginform )
        self.browser["loginName"] = self.var_blog_user.get()
        self.browser["loginPass"] = self.var_blog_password.get()
        self.browser.submit()

    def __microblog_login(self):
        self.browser.open( 'http://blog.sina.com.cn' )
        self.browser.select_form( predicate=predicate_loginform )
        self.browser["loginName"] = self.var_microblog_user.get()
        self.browser["loginPass"] = self.var_microblog_password.get()
        self.browser.submit()

    def post_blog(self, title, content ):
        self.btn_blog_state.config( bg="yellow" )
        try:
            self.__blog_login()
            self.browser.open( 'http://control.blog.sina.com.cn/admin/article/article_add.php?index' )
        except:
            self.btn_blog_state.config( bg="red" )
        else:
            self.btn_blog_state.config( bg="blue" )

    def post_microblog(self, content ):
        self.btn_microblog_state.config( bg="yellow" )
        try:
            self.__microblog_login()
        except:
            self.btn_microblog_state.config( bg="red" )
        else:
            self.btn_microblog_state.config( bg="blue" )

    def display_blog_option(self, father):
        base_client.display_blog_option( self, father, "新浪", self.call_browser )
        self.var_x_cms_flag = Tkinter.StringVar()
        self.var_x_cms_flag.set( self.config["x_cms_flag"] )
        Tkinter.OptionMenu( father, self.var_x_cms_flag,"允许所有人评论","不允许匿名评论","不允许评论" ).grid( row=0, column=6, sticky=Tkinter.W )
        Tkinter.Checkbutton( father, text="文章仅自己可见" ).grid( row=0, column=7, sticky=Tkinter.W )
        Tkinter.Checkbutton( father, text="禁止转载" ).grid( row=0, column=8, sticky=Tkinter.W )

    def display_microblog_option(self, father):
        base_client.display_microblog_option( self, father, "新浪", self.call_browser )

    def call_browser(self):
        webbrowser.open_new_tab('http://login.sina.com.cn/member/my.php?entry=sso')

    def update_config(self):
        if "var_blog_user" in dir(self):
            self.config["blog_user"] = self.var_blog_user.get()
            self.config["blog_password"] = self.var_blog_password.get()
            self.config["x_cms_flag"] = self.var_x_cms_flag.get()
        if "var_microblog_user" in dir(self):
            self.config["microblog_user"] = self.var_microblog_user.get()
            self.config["microblog_password"] = self.var_microblog_password.get()
